{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9102f18f-4193-43e1-a206-50f302c5473a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc6afa9c-f5a9-46f8-935f-644c31c7bd8d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mdFilename='template_match.md'\n",
    "charpterName='template_match'\n",
    "lines=[]\n",
    "with open(mdFilename,'r',encoding='utf-8') as f:\n",
    "    lines=f.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "443c2ac8-56bd-424d-86c5-9e2b3b8a0f9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def changeOutFilename(temstr):\n",
    "    def fun(tem):\n",
    "        tem=str(tem.group())\n",
    "        res='images_{}/output/{}'.format(charpterName,tem)\n",
    "        return res\n",
    "    \n",
    "    res=re.sub('output\\w+.png',fun,temstr)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a2b7b983-2896-4b5d-afa2-dd6e8ad0390c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def changeImageFilename(temstr):\n",
    "    def fun(tem):\n",
    "        filename=tem.group(2)\n",
    "        res='images_{}/{}'.format(charpterName,filename)\n",
    "        return res\n",
    "\n",
    "    res=re.sub('(images)/(\\w+.\\w{3,3})',fun,temstr)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "28dc405c-b12c-4114-9c5f-2b884021fe26",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for index,line in enumerate(lines):\n",
    "    lines[index]=changeOutFilename(line)\n",
    "    lines[index]=changeImageFilename(lines[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f4d03c81-0622-48a6-9be4-83bd9adba4a6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# for index,line in enumerate(lines):\n",
    "#     print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a9c65cf7-dfd8-4e55-9f13-4be2a3798daa",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(mdFilename,'w',encoding='utf-8') as f:\n",
    "    f.writelines(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "007d944a-2d9e-429c-b7d2-a8120aec0820",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
